<template>
	<view class="home">
		<scroll-view scroll-x class="navscroll">
			<view class="item" :class="index==navIndex ? 'active':''" v-for="(item,index) in navArr" :key="item.id"
				@click="clickNav(index,item.id)">{{item.classname}}
			</view>
		</scroll-view>
		<view class="content">
			<view class="row" v-for="item in newArr" :key="item.id">
				<newbox :item="item" @click.native="goDetail(item)"></newbox>
			</view>
		</view>
		<view class="nodata" v-if="!newArr.length">
			<image src="../../static/没数据.png" mode="widthFix"></image>
		</view>
		<view class="loading" v-if="newArr.length">
			<view v-if="loading==1">数据加载中...</view>
			<view v-else-if="loading==2">---没有更多了---</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				navIndex: 0,
				navArr: [],
				newArr: [],
				// 页码
				currentPage: 1,
				loading: 1 //0默认 1加载中 2没有更多了
			}
		},
		onLoad() {
			this.getNavData(),
				this.getNewData()
		},
		onReachBottom() {
			if (this.loading == 2) {
				return
			}
			this.currentPage++;
			this.loading = 1
			this.getNewData()
		},
		methods: {
			clickNav(index, id) {
				this.navIndex = index
				this.currentPage = 1
				this.loading = 0
				this.newArr = []
				this.getNewData(id)
			},
			goDetail(item) {
				console.log(item);
				uni.navigateTo({
					url: `/pages/detail/detail?cid=${item.classid}&id=${item.id}`
				})
			},

			// 导航栏的网络请求
			getNavData() {
				uni.request({
					url: "https://ku.qingnian8.com/dataApi/news/navlist.php",
					success: res => {
						this.navArr = res.data
					},
				})
			},

			// 新闻列表的数据请求
			getNewData(id = 50) {
				uni.request({
					url: "https://ku.qingnian8.com/dataApi/news/newslist.php",
					data: {
						cid: id,
						page: this.currentPage
					},
					success: res => {
						if (res.data.length == 0) {
							this.loading = 2
						}
						this.newArr = [...this.newArr, ...res.data]
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.home {
		.navscroll {
			height: 100rpx;
			background: #F7F8FA;
			white-space: nowrap;
			position: fixed;
			top: var(--window-top);
			left: 0;
			z-index: 10;

			// 滚动条消失
			/deep/ ::-webkit-scrollbar {
				width: 4px !important;
				height: 1px !important;
				overflow: auto !important;
				background: transparent !important;
				-webkit-appearance: auto !important;
				display: block;
			}

			.item {
				font-size: 40rpx;
				display: inline-block;
				line-height: 100rpx;
				padding: 0 30rpx;
				color: #333;

				&.active {
					color: #1296db;
				}
			}
		}

		.content {
			padding: 30rpx;
			padding-top: 130rpx;

			.row {
				border-bottom: 1px dashed #efefef;
				padding: 20rpx 0;
			}
		}

		.loading {
			text-align: center;
			font-size: 26rpx;
			color: #888;
			line-height: 2em;
		}

		.nodata {
			display: flex;
			justify-content: center;

			image {
				width: 360rpx;
			}
		}
	}
</style>